package com.edmendst.xmall.controller;

import com.edmendst.xmall.dto.FixProduct;
import com.edmendst.xmall.dto.ProductCategory;
import com.edmendst.xmall.dto.ProductSellInfo;
import com.edmendst.xmall.pojo.Category;
import com.edmendst.xmall.service.ProductService;
import com.edmendst.xmall.service.ShopService;
import com.edmendst.xmall.service.UserService;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;
/**
 * @author mjz
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @Autowired
    private ShopService shopService;

    //更新（新建）商品分类名
    @PutMapping("/category")
    public String updateProductCategory(@RequestBody ProductCategory productCategory) {
        return productService.updateProductCategory(productCategory);
    }
//    //删除商品分类名
//    int updateProductCategoryName(Integer categoryId);
    @DeleteMapping("/category")
    public int updateProductCategoryName(@RequestBody @JsonProperty("categoryId") Integer categoryId) {
        return productService.deleteProductCategoryName(categoryId);
    }

    //    //输出0级商品分类表
//    List<Category> selectProductCategoryOfTop(Integer parentId);
    @GetMapping("/category")
    List<Category> selectProductCategoryOfTop() {
        List<Category> categories = productService.selectProductCategoryOfTop();
        return categories;
    }

//    //出售商品
//    String productSell(ProductSellInfo productSellInfo, MultipartFile productCoverImage);
    @PostMapping
    public String productSell( @RequestPart(value = "productInfo", required = true)
                                   @Valid ProductSellInfo productSellInfo,
                               @RequestPart(value = "productCoverImage", required = true)
                               @Valid List<MultipartFile> productCoverImage) {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        if (shopService.selectShopIdByUsername(username) == null) {
            return "请先创建店铺";
        }
        int shopIdByUsername = shopService.selectShopIdByUsername(username);
        productSellInfo.setShopId(shopIdByUsername);

        return productService.productSell(productSellInfo, productCoverImage);
    }

    //删除商品
    @DeleteMapping("/{productId}")
    public int deleteProduct(@PathVariable Integer productId) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        return productService.deleteProduct(productId, username);
    }

//
//    //通过分类查询商品信息
//    List<ProductSellInfo> selectProductByCategoryId(Integer categoryId);

    @GetMapping("/category/{categoryId}")
    List<ProductSellInfo> selectProductByCategoryId( @PathVariable Integer categoryId){
        return productService.selectProductByCategoryId(categoryId);
    }
//
    //关键词查询商品
//    List<ProductSellInfo> selectProductByKeyWord(String productName);

    @GetMapping("/keyword/{productName}")
    List<ProductSellInfo> selectProductByKeyWord( @PathVariable String productName){
        return productService.selectProductByKeyWord(productName);
    }
//
//    //输出商品信息
//    List<ProductSellInfo> outputProduct();
    @GetMapping
    List<ProductSellInfo> outputProduct(){
        return productService.outputProduct();
    }

    //输出所有信息
    @GetMapping("/all")
    List<ProductSellInfo> outputAllProduct(){
        return productService.outputAllProduct();
    }

//    //通过卖家查询商品信息
//    List<ProductSellInfo> getProductBySeller(String username);
    @GetMapping("/seller/{shopId}")
    List<ProductSellInfo> getProductBySeller(@PathVariable Integer shopId){
        return productService.getProductBySeller(shopId);
    }

//
//    //编辑商品价格
//    String updateProductPrice(Integer productId, BigDecimal productPrice);
    @PutMapping("/price")
    String updateProductPrice(@RequestBody FixProduct fixProduct) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        return productService.updateProductPrice(fixProduct.getProductId(), fixProduct.getProductPrice(), username);
    }
//
//    //编辑商品介绍
//    String updateProductDescription(Integer productId, String productDescription);
    @PutMapping("/description")
    String updateProductDescription(@RequestBody FixProduct fixProduct) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        return productService.updateProductDescription(fixProduct.getProductId(), fixProduct.getProductDescription(),username);
    }
//
//    //查看商品库存
//    ProductSellInfo getStock(Integer productId);
    @GetMapping("/stock/{productId}")
    ProductSellInfo getStock(@PathVariable Integer productId) {
        return productService.getStock(productId);
    }
//
//    //通过id查询商品信息
//    Product getProductById(Integer productId);
    @GetMapping("/{productId}")
    ProductSellInfo getProductById(@PathVariable Integer productId) {
        return productService.getProductById(productId);
    }

    @PutMapping("/onlineStatus")
    String updateProductOnlineStatus(@RequestBody ProductSellInfo productSellInfo) {

        return productService.updateProductOnlineStatus(productSellInfo.getProductId(), productSellInfo.getProductOnlineStatus());
    }



}
